﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Model
{
	public class NestedExpression : Expression
	{

		private Expression leftSide;
		private Expression rightSide;
		private string boolComp;

		/**
		 * Erzeugt eine verschachtelte Expression, die aus zwei einzelnen bool
		 * Expressions besteht und einem bool Operator.
		 * 
		 * @param leftSide linke Expression
		 * @param boolComp bool Operator. Muss "and", "or" oder "xor" entsprechen.
		 * @param rightSide rechte Expression
		 * @throws Exception
		 */
		public NestedExpression(Expression leftSide, string boolComp,
				Expression rightSide)
		{
			if (leftSide == null || boolComp == null || rightSide == null)
				throw new Exception("Mindestens ein übergebener Parameter ist null");

			if (!(boolComp.Equals("and") || boolComp.Equals("or")
					|| boolComp.Equals("xor")))
				throw new Exception(boolComp + " ist kein gültiger Operator.");

			this.leftSide = leftSide;
			this.rightSide = rightSide;
			this.boolComp = boolComp;

		}

		public override bool evaluate(Cell cellToEval)
		{
			this.cellEvaluate = cellToEval;
			leftSide.EvaluationCell = cellToEval;
			rightSide.EvaluationCell = cellToEval;
			switch (boolComp)
			{
				case "and":
					return leftSide.and(rightSide);
				case "or":
					return leftSide.or(rightSide);
				case "xor":
					return leftSide.xor(rightSide);
				default:
					throw new Exception("evaluation in " + this + " failed.");
			}

		}

	}
}
